package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.BiCopiasEmprestadasDAO;
import br.urca.model.BiCopiasEmprestadas;

@Transactional
public class JPABiCopiasEmprestadasDAO implements BiCopiasEmprestadasDAO {
	
	private EntityManager manager;
	
	public JPABiCopiasEmprestadasDAO() {
		this(null);
	}
	
	@Inject
	public JPABiCopiasEmprestadasDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<BiCopiasEmprestadas> getPorId(Character codTipoMaterial, int codNumeroMaterial) {
		try {
			return this.manager
					.createQuery(
							"SELECT b FROM BiCopiasEmprestadas b WHERE b.biCopiasEmprestadasPK.codTipoMaterial = :codTipoMaterial "
									+ " AND b.biCopiasEmprestadasPK.codNumeroMaterial = :codNumeroMaterial ",
							BiCopiasEmprestadas.class)
					.setParameter("codTipoMaterial", codTipoMaterial)
					.setParameter("codNumeroMaterial", codNumeroMaterial)
					.getResultList();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public BiCopiasEmprestadas getPorId(Character codTipoMaterial, int codNumeroMaterial,
			String codRegistro) {
		try {
			return this.manager 
					.createQuery(
							"SELECT b FROM BiCopiasEmprestadas b WHERE b.biCopiasEmprestadasPK.codTipoMaterial = :codTipoMaterial "
									+ " AND b.biCopiasEmprestadasPK.codNumeroMaterial = :codNumeroMaterial "
									+ " AND b.biCopiasEmprestadasPK.codRegistro = :codRegistro ",
							BiCopiasEmprestadas.class)
					.setParameter("codTipoMaterial", codTipoMaterial)
					.setParameter("codNumeroMaterial", codNumeroMaterial)
					.setParameter("codRegistro", codRegistro)
					.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

}
